package zjl.util;

public class Pair<T1, T2> {
    private final T1 o1;
    private final T2 o2;

    public Pair(T1 o1, T2 o2) {
        this.o1 = o1;
        this.o2 = o2;
    }

    public T1 first() {
        return this.o1;
    }

    public T2 second() {
        return this.o2;
    }

    public static boolean same(Object o1, Object o2) {
        return o1 == null ? o2 == null : o1.equals(o2);
    }

    @Override
    public boolean equals(Object o) {
        if (!(o instanceof Pair)) {
            return false;
        }

        Pair<T1, T2> p = (Pair<T1, T2>)o;
        return same(p.o1, this.o1) && same(p.o2, this.o2);
    }

    @Override
    public int hashCode() {
        int code = 0;
        if (o1 != null)
            code = o1.hashCode();
        if (o2 != null)
            code = code / 2 + o2.hashCode() / 2;
        return code;
    }

    @Override
    public String toString() {
        return "Pair<" + o1 + ", " + o2 + ">";
    }
}
